<template>
  <div>
    <Category :scene="scene" />
    <el-card style="margin: 10px 0px">
      <!-- v-if|v-show都可以实现显示与隐藏 -->
      <div v-show="scene == 0">
        <el-button
          icon="Plus"
          type="primary"
          size="default"
          :disabled="categoryData.c3Id ? false : true"
          @click="addSpu"
        >
          添加SPU
        </el-button>
        <el-table :data="records" style="width: 100%; margin: 10px 0px" border>
          <el-table-column
            label="序号"
            width="80px"
            align="center"
            type="index"
          ></el-table-column>
          <el-table-column
            label="SPU名称"
            align="center"
            prop="spuName"
          ></el-table-column>
          <el-table-column
            label="SPU描述"
            align="center"
            prop="description"
            show-overflow-tooltip
          ></el-table-column>
          <el-table-column label="操作" width="220px" align="center">
            <template #="{ row, column, $index }">
              <el-button
                icon="Plus"
                type="primary"
                size="small"
                title="添加SKU"
                @click="addSku(row)"
              ></el-button>
              <el-button
                icon="Edit"
                type="warning"
                size="small"
                title="修改SPU"
                @click="updateSpu(row)"
              ></el-button>
              <el-button
                icon="View"
                type="info"
                size="small"
                title="查看SKU列表"
                @click="findSku(row)"
              ></el-button>
              <el-popconfirm
                icon="Delete"
                :title="`确定删除${row.spuName}吗?`"
                width="200px"
                @confirm="deleteSpu(row)"
              >
                <template #reference>
                  <el-button
                    icon="Delete"
                    type="danger"
                    size="small"
                    title="删除SPU"
                  ></el-button>
                </template>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页器组件-->
        <el-pagination
          v-model:current-page="pageNo"
          v-model:page-size="pageSize"
          :page-sizes="[3, 5, 7, 9]"
          :background="true"
          layout="prev, pager, next, jumper, ->, sizes, total"
          :total="total"
          @current-change="getHasSpu"
          @size-change="handleSizeChange"
        />
      </div>
      <!-- 添加/修改spu的子组件 -->
      <spuForm
        ref="spu"
        v-show="scene == 1"
        @changeScene="changeScene"
      ></spuForm>
      <!-- 添加sku的子组件 -->
      <skuForm
        ref="sku"
        v-show="scene == 2"
        @changeScene="changeScene"
      ></skuForm>
      <el-dialog title="SKU列表" v-model="show">
        <el-table border :data="skuArr">
          <el-table-column
            label="SKU名称"
            prop="skuName"
            header-align="center"
          ></el-table-column>
          <el-table-column
            label="SKU价格"
            prop="price"
            align="center"
          ></el-table-column>
          <el-table-column
            label="SKU重量"
            prop="weight"
            align="center"
          ></el-table-column>
          <el-table-column label="SKU图片" align="center">
            <template #="{ row, $index }">
              <img
                :src="row.skuDefaultImg"
                alt="糟糕！图片找不到了"
                style="width: 100px; height: 100px"
              />
            </template>
          </el-table-column>
        </el-table>
      </el-dialog>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { ref, watch, onBeforeUnmount } from 'vue'

import categoryDataStore from '@/store/modules/category'
import { reqSkuList, reqHasSpu, reqRemoveSpu } from '@/api/product/spu/index'
import type {
  skuInfoData,
  HasSpuResponseData,
  Records,
  SpuData,
  SkuData,
} from '@/api/product/spu/type'
// 引入子组件
import spuForm from './spuForm.vue'
import skuForm from './skuForm.vue'
import { ElMessage } from 'element-plus'
const categoryData = categoryDataStore()
// 监听仓库内三级分类Id的变化；发请求获取对应的SPU信息
const scene = ref<number>(0) // 0:显示已有SPU 1:添加或者修改已有SPU 2:添加SKU的结构
const total = ref<number>(0)
const pageNo = ref<number>(1)
const pageSize = ref<number>(3)
// 存储已有的spu的数据
const records = ref<Records>([])
// 获取子组件实例SpuForm
const spu = ref<any>()
// 获取子组件实例SkuForm
const sku = ref<any>()
// sku列表数据
const skuArr = ref<SkuData[]>([])
// 控制dialog显隐
const show = ref<boolean>(false)
watch(
  () => categoryData.c3Id,
  () => {
    // 保证三级分类得有才能发请求
    if (!categoryData.c3Id) return
    getHasSpu()
  },
)
// 获取全部已有的SPU
const getHasSpu = async (pager = 1) => {
  pageNo.value = pager
  const result: HasSpuResponseData = await reqHasSpu(
    pageNo.value,
    pageSize.value,
    categoryData.c3Id,
  )
  if (result.code == 200) {
    // 存spu数据
    records.value = result.data.records
    total.value = result.data.total
  }
}
// 分页器下拉菜单发生变化时触发
const handleSizeChange = () => {
  getHasSpu()
}
// 添加SPU单击事件
const addSpu = () => {
  // 切换为场景1
  scene.value = 1
  // 调用子组件的方法初始化数据
  spu.value.initAddSpu(categoryData.c3Id)
}
// 修改已有的SPU
const updateSpu = (row: SpuData) => {
  scene.value = 1
  // 获取子组件暴露的方法 initHasSpuData
  // console.log(spu.value)
  // 父组件调用子组件的方法
  spu.value.initHasSpuData(row)
}
// 子组件SpuForm绑定自定义事件：目前是让子组件通知父组件切换场景为0
const changeScene = (obj: any) => {
  // 子组件点击spuForm点击取消变为场景0
  scene.value = obj.flag
  // 再次获取全部已有的SPU;如果是更新就传pageNo
  if (obj.params == 'update') {
    getHasSpu(pageNo.value)
  } else {
    getHasSpu()
  }
}
const addSku = (row: SpuData) => {
  //   console.log(row);
  scene.value = 2
  // 调用子组件的方法初始化sku数据
  sku.value.initSkuData(categoryData.c1Id, categoryData.c2Id, row)
}

const findSku = async (row: SpuData) => {
  const result: skuInfoData = await reqSkuList(row.id as number)
  if (result.code == 200) {
    show.value = true
    skuArr.value = result.data
  }
}

const deleteSpu = async (row: SpuData) => {
  const result: any = await reqRemoveSpu(row.id as number)
  if (result.code == 200) {
    ElMessage({
      type: 'success',
      message: '删除成功',
    })
    // 当前的数据长度大于1;页码是当前页;否则是上一页
    getHasSpu(records.value.length > 1 ? pageNo.value : pageNo.value - 1)
  } else {
    ElMessage({
      type: 'error',
      message: '删除失败',
    })
  }
}

// 路由组建销毁的时候，把仓库分类相关的数据清空
onBeforeUnmount(() => {
  categoryData.$reset()
})
</script>

<style scoped></style>
